//
//  FindListViewController.m
//  B段项目
//
//  Created by 徐茂怀 on 15/11/7.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "FindListViewController.h"
#import "FitHeader.h"
#import "XMHNetWorkongMethods.h"
#import "NSXFirstPageWEBViewController.h"
static NSInteger flag = 1;
@interface FindListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)UIAlertController *alertController;
@end

@implementation FindListViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSMutableArray array];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
       
    _progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"加载中,不要着急";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_progressView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    _tableView.tableFooterView = v;
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArray removeAllObjects];
        flag = 1;
        [self getData];
    }];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        flag += 1;
        [self getData];
        [self reloadInputViews];
    }];
    [self getData];
}

-(void)getData
{
    NSString *str = [NSString stringWithFormat:@"%ld",flag];
    NSString *string = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.list.php?keywords=%@&&p=%@&pagesize=12",_name,str];
   [XMHNetWorkongMethods getDataVyString:string BodyString:nil WithDataBlock:^(id data) {
       NSArray *array = [data objectForKey:@"results"];
       for (NSDictionary *dic in array) {
           NSXFirstPageListModel *model = [[NSXFirstPageListModel alloc]init];
           [model setValuesForKeysWithDictionary:dic];
           [_dataArray addObject:model];
       }
       [_tableView.header endRefreshing];
       [_tableView.footer endRefreshing];
       [_progressView removeFromSuperview];
       [_tableView beginUpdates];
       [_tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft];
       [_tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];
       [_tableView endUpdates];
       [_tableView reloadData];
   } WithFailure:^(NSError *error) {
       if (!_dataArray.count) {
           
           _alertController = [UIAlertController alertControllerWithTitle:@"网络错误" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
           [self presentViewController:_alertController animated:YES completion:nil];
           UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
               [_alertController removeFromParentViewController];
               [_progressView removeFromSuperview];
               [_tableView.header endRefreshing];
               [_tableView.footer endRefreshing];
           }];
           UIAlertAction *going = [UIAlertAction actionWithTitle:@"继续加载" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
               [self getData];
               [_alertController removeFromParentViewController];
           }];
           [_alertController addAction:going];
           [_alertController addAction:ok];
       }

   }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FitHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    XMHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[XMHTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_dataArray.count == 0) {
        return cell;
    }else {
    cell.model = _dataArray[indexPath.row];
    if ([[_dataArray[indexPath.row] category] isEqualToString:@""]) {
        [cell.categoryLabel removeFromSuperview];
    }
    if ([[_dataArray[indexPath.row] effect1] isEqualToString:@""]) {
        [cell.effectLabel  removeFromSuperview];
    }
    if ([[_dataArray[indexPath.row] age] isEqualToString:@""]) {
        [cell.ageLabel removeFromSuperview];
    }
    return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArray.count) {
        
        self.hidesBottomBarWhenPushed = YES;
        NSXFirstPageWEBViewController *vc = [[NSXFirstPageWEBViewController alloc]init];
        vc.NSXModel = _dataArray[indexPath.row];
        vc.idString = [_dataArray[indexPath.row] ID];
        vc.modelString = [_dataArray[indexPath.row] title];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
